//
//  RCHomeTableViewController.swift
//  RCWeibo
//
//  Created by CL on 15/12/13.
//  Copyright © 2015年 CL. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class RCHomeTableViewController: RCBaseTableViewController {
    
    private let HomeCellID = "HomeCellID"
    private lazy var statuses = [RCStatus]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            //设置访客界面
            visitorLoginView?.setWithInfo("关注一些人，回这里看看有什么惊喜", imageName: nil)
            
            return
        }
        
        //注册cell
        tableView.registerClass(RCStatusCell.self, forCellReuseIdentifier: HomeCellID)
        
        loadData()
        
    }
    
    
    private func loadData(){
        
        let AFN = AFHTTPSessionManager()
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        guard let token = RCUserAccountViewModel().userToken else {
            
            print("token为空")
            SVProgressHUD.showInfoWithStatus("请重新登录")
            return
            
        }
        
        let param = ["access_token" : token]
        AFN.GET(urlString, parameters: param, progress: { (_) -> Void in
            
            
            }, success: { (task, result) -> Void in
                
                guard let dict = result as? [String : AnyObject] else {
                    
                    print("statuses不合法")
                    SVProgressHUD.showErrorWithStatus("获取数据出错")
                    return
                }
                
                //获取成功
                if let arr = dict["statuses"] as? [[String : AnyObject]] {
                    
                    //字典转模型
                    //创建临时数组，用来存饭模型
                    var list = [RCStatus]()
                    //遍历获取的数组。
                    for item in arr {
                        
                        //将数组里的字典元素通过kvc赋值给模型
                        let s = RCStatus(dict: item)
                        //将模型添加到临时数组
                        list.append(s)
                    }
                    // 将临时数组赋值给statuses数组
                    self.statuses = list
                    print(list)
                    // 刷新tableView
                    self.tableView.reloadData()
                }
                
                
            }) { (task, error) -> Void in
                
                
                print(error)
        }
        
        
    }

    
}
// MARK: - 数据源协议方法
extension RCHomeTableViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! RCStatusCell
        
        cell.textLabel?.text = statuses[indexPath.row].user?.name
        
        return cell
    }
    
    
    
}
